package com.vincent.seeyou.ui;

import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.NfcEvent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.widget.Toast;

import com.vincent.seeyou.SYApplication;
import com.vincent.seeyou.model.User;
import com.vincent.seeyou.nfc.SYNFCManager;
import com.vincent.seeyou.util.CollectionUtils;

import java.nio.charset.Charset;
import java.util.List;

import cn.bmob.im.BmobChatManager;
import cn.bmob.im.BmobUserManager;
import cn.bmob.im.bean.BmobChatUser;
import cn.bmob.im.config.BmobConfig;
import cn.bmob.im.db.BmobDB;
import cn.bmob.im.util.BmobLog;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by vincent on 14-9-1.
 */
public class BaseActivity extends FragmentActivity{


    public BmobUserManager bmobUserManager;
    public BmobChatManager bmobChatManager;
    public SYApplication syApplication;
    public SYNFCManager synfcManager;

    protected int mScreenWidth;
    protected int mScreenHeight;

    public Toast mToast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bmobUserManager = BmobUserManager.getInstance(this);
        bmobChatManager = BmobChatManager.getInstance(this);
        syApplication = SYApplication.getInstance();
        synfcManager = SYNFCManager.getInstance();

        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        mScreenHeight = displayMetrics.heightPixels;
        mScreenWidth = displayMetrics.widthPixels;


    }


    /**
     * 用于登陆或者自动登陆情况下的用户资料及好友资料的检测更新
     */
    public void updateUserInfos(){
        //更新地理位置信息
        updateUserLocation();
        //查询该用户的好友列表(这个好友列表是去除黑名单用户的哦),目前支持的查询好友个数为100，如需修改请在调用这个方法前设置BmobConfig.LIMIT_CONTACTS即可。
        //这里默认采取的是登陆成功之后即将好于列表存储到数据库中，并更新到当前内存中,
        bmobUserManager.queryCurrentContactList(new FindListener<BmobChatUser>() {

            @Override
            public void onError(int arg0, String arg1) {
                // TODO Auto-generated method stub
                if(arg0== BmobConfig.CODE_COMMON_NONE){
                    ShowLog(arg1);
                }else{
                    ShowLog("查询好友列表失败："+arg1);
                }
            }

            @Override
            public void onSuccess(List<BmobChatUser> arg0) {
                // TODO Auto-generated method stub
                // 保存到application中方便比较
                ShowLog("查询好友列表成功");
                SYApplication.getInstance().setContactList(CollectionUtils.list2map(arg0));
            }
        });
    }


    public void updateUserLocation(){
        if(SYApplication.lastPoint!=null){
            String saveLatitude  = syApplication.getLatitude();
            String saveLongtitude = syApplication.getLongtitude();
            String newLat = String.valueOf(SYApplication.lastPoint.getLatitude());
            String newLong = String.valueOf(SYApplication.lastPoint.getLongitude());
			ShowLog("saveLatitude ="+saveLatitude+",saveLongtitude = "+saveLongtitude);
			ShowLog("newLat ="+newLat+",newLong = "+newLong);
            if(!saveLatitude.equals(newLat)|| !saveLongtitude.equals(newLong)){//只有位置有变化就更新当前位置，达到实时更新的目的
                final User user = (User) bmobUserManager.getCurrentUser(User.class);
                user.setLocation(SYApplication.lastPoint);
                user.update(this, new UpdateListener() {
                    @Override
                    public void onSuccess() {
                        // TODO Auto-generated method stub
                        SYApplication.getInstance().setLatitude(String.valueOf(user.getLocation().getLatitude()));
                        SYApplication.getInstance().setLongtitude(String.valueOf(user.getLocation().getLongitude()));
						ShowLog("经纬度更新成功");
                    }
                    @Override
                    public void onFailure(int code, String msg) {
                        // TODO Auto-generated method stub
						ShowLog("经纬度更新 失败:"+msg);
                    }
                });
            }else{
				ShowLog("用户位置未发生过变化");
            }
        }
    }


    public void startAnimActivity(Class<?> cla) {
        this.startActivity(new Intent(this, cla));
    }

    public void startAnimActivity(Intent intent) {
        this.startActivity(intent);
    }

    public void ShowToast(final String text) {
        if (!TextUtils.isEmpty(text)) {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    if (mToast == null) {
                        mToast = Toast.makeText(getApplicationContext(), text,
                                Toast.LENGTH_LONG);
                    } else {
                        mToast.setText(text);
                    }
                    mToast.show();
                }
            });

        }
    }

    public void ShowToast(final int resId) {
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                if (mToast == null) {
                    mToast = Toast.makeText(BaseActivity.this.getApplicationContext(), resId,
                            Toast.LENGTH_LONG);
                } else {
                    mToast.setText(resId);
                }
                mToast.show();
            }
        });
    }

    public void ShowLog(String msg){
        BmobLog.i(msg);
    }




}
